package com.lill.sale.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lill.common.component.util.IdUtil;
import com.lill.org.model.CompanyBranchModel;
import com.lill.org.model.PersonModel;
import com.lill.org.service.ICompanyBranchService;
import com.lill.org.service.IPersonService;
import com.lill.product.model.ProductBaseModel;
import com.lill.product.service.IProductBaseService;
import com.lill.sale.dao.ISaleBookDao;
import com.lill.sale.dao.ISaleBoutiqueDao;
import com.lill.sale.dao.ISaleCarDao;
import com.lill.sale.dao.ISaleCustomerDao;
import com.lill.sale.model.SaleBookModel;
import com.lill.sale.model.SaleBoutiqueModel;
import com.lill.sale.model.SaleCarModel;
import com.lill.sale.model.SaleCustomerModel;
import com.lill.sale.service.ISaleBookService;
import com.lill.sale.util.WordTableCreateUtil;
import com.lill.stock.model.StockRealModel;
import com.lill.stock.service.IStockRealService;

/**
 * 销售台账Service实现类
 *
 * @author lill
 * @date 2021-09-25 11:41:47
 */
@Service("com.lill.sale.service.impl.SaleBookServiceImpl")
public class SaleBookServiceImpl implements ISaleBookService {
	
    @Autowired
    private ISaleBookDao saleBookDao;
    @Autowired
    private ISaleCustomerDao saleCustomerDao;
    @Autowired
    private ICompanyBranchService branchService;
    @Autowired
    private IPersonService personService;
    @Autowired
    private ISaleCarDao saleCarDao;
    @Autowired
    private ISaleBoutiqueDao saleBoutiqueDao;
    @Autowired
    private IStockRealService stockRealService;
    @Autowired
    private IProductBaseService productBaseService;
    
    private static String headBgColor="FFFFFF";

    public SaleBookModel queryObject(String id) {
    	SaleBookModel bookModel=saleBookDao.queryObject(id);
    	getSaleBookDetail(bookModel);
        return bookModel;
    }
    
    public List<SaleBookModel> queryList(Map<String, Object> map) {
    	List<SaleBookModel> bookList=saleBookDao.queryList(map);
    	if(null!=bookList&&!bookList.isEmpty()){
//    		Map<String, PersonModel> personMap=new HashMap<String, PersonModel>();
    		Map<String, SaleCustomerModel> customerMap=new HashMap<String, SaleCustomerModel>();
    		for (SaleBookModel bookModel : bookList) {
//    			if(StringUtils.isNotEmpty(bookModel.getPersonId())){
//    				if(personMap.containsKey(bookModel.getPersonId())){
//    					PersonModel personModel=personMap.get(bookModel.getPersonId());
//    					bookModel.setPersonName(personModel.getName());
//						bookModel.setPersonPhone(personModel.getPhone());
//    				}else{
//    					PersonModel personModel=personService.queryObject(bookModel.getPersonId());
//    					if(null!=personModel){
//    						personMap.put(bookModel.getPersonId(), personModel);
//    						bookModel.setPersonName(personModel.getName());
//    						bookModel.setPersonPhone(personModel.getPhone());
//    					}
//    				}
//    	    	}
    			if(StringUtils.isNotEmpty(bookModel.getCustomerId())){
    				if(customerMap.containsKey(bookModel.getCustomerId())){
    					bookModel.setSaleCustomerInfo(customerMap.get(bookModel.getCustomerId()));
//    					bookModel.setCustomerName(customerMap.get(bookModel.getCustomerId()).getName());
    				}else{
    					SaleCustomerModel saleCustomer=saleCustomerDao.queryObject(bookModel.getCustomerId());
    					if(null!=saleCustomer){
    						customerMap.put(bookModel.getCustomerId(), saleCustomer);
    						bookModel.setSaleCustomerInfo(saleCustomer);
//    						bookModel.setCustomerName(saleCustomer.getName());
    					}
    				}
            	}
			}
    	}
        return bookList;
    }

    public int queryTotal(Map<String, Object> map) {
        return saleBookDao.queryTotal(map);
    }

    public int save(SaleBookModel saleBook) {
    	boolean operSave=true;
    	int result=0;
    	if(StringUtils.isEmpty(saleBook.getId())){
    		saleBook.setId(IdUtil.createIdbyUUID());
    		saleBook.setBookNo("HTSCSUY"+IdUtil.createIdByDate());
    	}else{
    		operSave=false;
    	}
    	if(operSave){
    		result=saleBookDao.save(saleBook);
        }else{
        	result=saleBookDao.update(saleBook);
        }
        if(result>0&&CollectionUtils.isNotEmpty(saleBook.getSaleCarList())){
        	List<SaleBoutiqueModel> boutiqueList=new ArrayList<SaleBoutiqueModel>();
        	for (SaleCarModel saleCar : saleBook.getSaleCarList()) {
        		saleCar.setId(IdUtil.createIdbyUUID());
        		saleCar.setSaleBookId(saleBook.getId());
        		if(CollectionUtils.isNotEmpty(saleCar.getSaleBoutiqueList())){
        			for (SaleBoutiqueModel saleBoutique : saleCar.getSaleBoutiqueList()) {
        				saleBoutique.setId(IdUtil.createIdbyUUID());
        				saleBoutique.setSaleBookId(saleBook.getId());
        				saleBoutique.setSaleCarId(saleCar.getId());
        				boutiqueList.add(saleBoutique);
        			}
        		}
        	}
        	saleCarDao.deleteByBookId(saleBook.getId());
        	saleBoutiqueDao.deleteByBookId(saleBook.getId());
        	saleCarDao.saveBatch(saleBook.getSaleCarList());
        	if (!boutiqueList.isEmpty()){
				saleBoutiqueDao.saveBatch(boutiqueList);
			}
        }
    	return result;
    }

    public int update(SaleBookModel saleBook) {
        return saleBookDao.update(saleBook);
    }

	public int deleteLogicByMap(Map<String, Object> map) {
		return saleBookDao.deleteLogicByMap(map);
	}
	
	private void getSaleBookDetail(SaleBookModel bookModel) {
		if(null!=bookModel){
    		if(StringUtils.isNotEmpty(bookModel.getPersonId())){
    			PersonModel personModel=personService.queryObject(bookModel.getPersonId());
    			if(null!=personModel){
    				bookModel.setPersonName(personModel.getName());
    				bookModel.setPersonPhone(personModel.getPhone());
    			}
    		}
    		if(StringUtils.isNotEmpty(bookModel.getCustomerId())){
    			SaleCustomerModel saleCustomer=saleCustomerDao.queryObject(bookModel.getCustomerId());
    			if(null!=saleCustomer){
    				CompanyBranchModel branchVo=branchService.queryObject(saleCustomer.getBranchId());
    				if(null!=branchVo){
    					saleCustomer.setBranchAllName(branchVo.getAllName());
    					saleCustomer.setBranchAddress(branchVo.getAddress());
    				}
    				bookModel.setSaleCustomerInfo(saleCustomer);
    				bookModel.setCustomerName(saleCustomer.getName());
    			}
    		}
    		
    		Map<String, Object> carMap=new HashMap<String, Object>();
    		carMap.put("saleBookId", bookModel.getId());
    		List<SaleCarModel> saleCarList=saleCarDao.queryList(carMap);
    		if(CollectionUtils.isNotEmpty(saleCarList)){
    			for (SaleCarModel saleCarModel : saleCarList) {
    				ProductBaseModel productBase=productBaseService.queryObject(saleCarModel.getProductCarId());
    				if(null!=productBase){
    					saleCarModel.setProductCarName(productBase.getAllName());
    					saleCarModel.setCarCode(productBase.getCode());
    				}
    				carMap=new HashMap<String, Object>();
    				carMap.put("saleCarId", saleCarModel.getId());
    				List<SaleBoutiqueModel> saleBoutiqueList=saleBoutiqueDao.queryList(carMap);
    				if(CollectionUtils.isNotEmpty(saleBoutiqueList)){
    					for (SaleBoutiqueModel saleBoutiqueModel : saleBoutiqueList) {
    						productBase=productBaseService.queryObject(saleBoutiqueModel.getBoutiqueId());
    						if(null!=productBase){
    							saleBoutiqueModel.setBoutiqueName(productBase.getAllName());
    						}
    						saleBoutiqueModel.setShowbp(true);
    					}
    					saleCarModel.setSaleBoutiqueList(saleBoutiqueList);
    				}
    				saleCarModel.setShowCar(true);
    			}
    			bookModel.setSaleCarList(saleCarList);
    		}
    	}
	}

	public int updateSate(Map<String, Object> mapParam) {
		return saleBookDao.updateSate(mapParam);
	}
	

	public int updateReceipt(Map<String, Object> mapParam, String personId) {
		int result=saleBookDao.updateSate(mapParam);
		if(result>0){
			SaleBookModel saleBook=saleBookDao.queryObject(mapParam.get("id"));
			SaleCustomerModel saleCustomuer=saleCustomerDao.queryObject(saleBook.getCustomerId());
			saleCustomuer.setState(2);
			saleCustomerDao.update(saleCustomuer);
			
			Map<String, Object> mapCar=new HashMap<String, Object>();
			mapCar.put("saleBookId", mapParam.get("id"));
			List<SaleCarModel> saleCarList=saleCarDao.queryList(mapCar);
			if(CollectionUtils.isNotEmpty(saleCarList)){
				mapCar.put("saleBookId", mapParam.get("id"));
				mapCar.put("state", 4);
				mapCar.put("oldState", 2);
				for (SaleCarModel saleCarModel : saleCarList) {
					if(StringUtils.isNotEmpty(saleCarModel.getVin())){
						mapCar.put("vin", saleCarModel.getVin());
						stockRealService.updateRealStock(mapCar);
					}
				}
			}
		}
		return result;
	}

	public int cancelSate(Map<String, Object> mapParam) {
		int result=saleBookDao.cancelSate(mapParam);
		if(result>0){
			Map<String, Object> carParam=new HashMap<String, Object>();
			carParam.put("saleBookId", mapParam.get("id"));
			List<SaleCarModel> saleCarList=saleCarDao.queryList(carParam);
			if(CollectionUtils.isNotEmpty(saleCarList)){
				for (SaleCarModel saleCar : saleCarList) {
					if(StringUtils.isEmpty(saleCar.getVin())){
						Map<String, Object> stockParam=new HashMap<String, Object>(4);
						stockParam.put("vin", saleCar.getVin());
						List<StockRealModel> stockRealList=stockRealService.queryList(stockParam);
						if(CollectionUtils.isNotEmpty(stockRealList)){
							for (StockRealModel stockRealModel : stockRealList) {
								stockRealService.updateCancelOrder(stockRealModel.getId());
							}
						}
					}
				}
			}
		}
		return result;
	}

	public XWPFDocument downLoadBookData(String id) {
		XWPFDocument workdocx=null;
		SaleBookModel bookModel=queryObject(id);
		if(null!=bookModel&&null!=bookModel.getSaleCustomerInfo()
				&&StringUtils.isNotEmpty(bookModel.getSaleCustomerInfo().getBranchAllName())){
			workdocx = new XWPFDocument();
			int rowNum=0;
			XWPFTable bookTable=WordTableCreateUtil.ctreateBaseTableWithBd(workdocx, 2, 9);
			XWPFTableRow rowData=bookTable.insertNewTableRow(rowNum);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, 
					bookModel.getSaleCustomerInfo().getBranchAllName()+"合同",true);
			for (int i = 0; i < 9; i++) {
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			}
			++rowNum;
			
			rowData=bookTable.insertNewTableRow(rowNum);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "供方");
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,
					bookModel.getSaleCustomerInfo().getBranchAllName(),true);
			for (int i = 0; i < 4; i++) {
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			}
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "合同编号",true);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,bookModel.getBookNo(),true);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			++rowNum;
			
			rowData=bookTable.insertNewTableRow(rowNum);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "购方");
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,
					bookModel.getSaleCustomerInfo().getName()+bookModel.getIdcard(),true);
			for (int i = 0; i < 4; i++) {
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			}
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "签订地点",true);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,
					bookModel.getSaleCustomerInfo().getBranchAddress(),true);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			++rowNum;
			
			for (SaleCarModel saleCar : bookModel.getSaleCarList()) {
				rowData=bookTable.insertNewTableRow(rowNum);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "车型");
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "代码",true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "配置",true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "内饰",true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "颜色",true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "数量",true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "整车单价",true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
				++rowNum;
				
				rowData=bookTable.insertNewTableRow(rowNum);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, saleCar.getCarModel());
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, saleCar.getCarCode(),true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, saleCar.getConfig(),true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, saleCar.getDesign(),true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, saleCar.getColour(),true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "1",true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, saleCar.getSumCost().toString(),true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
				++rowNum;
				
				rowData=bookTable.insertNewTableRow(rowNum);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "名称");
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "购买精品",true);
				for (int i = 0; i < 4; i++) {
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
				}
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "数量",true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "精品总价",true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
				++rowNum;
				
				if(CollectionUtils.isNotEmpty(saleCar.getSaleBoutiqueList())){
					for (int i = 0; i < saleCar.getSaleBoutiqueList().size(); i++) {
						rowData=bookTable.insertNewTableRow(rowNum);
						WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, String.valueOf(i+1));
						WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, 
								saleCar.getSaleBoutiqueList().get(i).getBoutiqueName(),true);
						for (int j = 0; j < 4; j++) {
							WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
						}
						WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, 
								saleCar.getSaleBoutiqueList().get(i).getNum().toString(),true);
						if(i==0){
							WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, 
									(saleCar.getBoutiqueSumCost()==null?"":saleCar.getBoutiqueSumCost().toString()),true);
						}else{
							WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"",true);
						}
						WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
						WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
						++rowNum;
					}
				}
				rowData=bookTable.insertNewTableRow(rowNum);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "购置税（代收）");
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, 
						(saleCar.getAcquireCost()==null?"":saleCar.getAcquireCost().toString()),true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "保险公司",true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, 
						(saleCar.getInsurancecompanyName()==null?"":saleCar.getInsurancecompanyName()),true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "");
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "交强险",true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, 
						(saleCar.getTrafficCost()==null?"":saleCar.getTrafficCost().toString()),true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "商业险",true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, 
						(saleCar.getInsuranceCost()==null?"":saleCar.getInsuranceCost().toString()),true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
				++rowNum;
				
				rowData=bookTable.insertNewTableRow(rowNum);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "上牌代办费");
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, 
						(saleCar.getPlateCost()==null?"":saleCar.getPlateCost().toString()),true);
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "注：因车辆保险必须直接实名支付给保险公司，此保险价格为预估，不含在合同总价中",true);
				for (int i = 0; i < 7; i++) {
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "");
				}
				++rowNum;
				
				if(1==saleCar.getPayment()){
					rowData=bookTable.insertNewTableRow(rowNum);
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "付款方式");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "全款",true);
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "其他费用",true);
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"",true);
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
					++rowNum;
				}else{
					rowData=bookTable.insertNewTableRow(rowNum);
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "付款方式");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "分期");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"首付车款");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,(saleCar.getDownPayment()==null?"":saleCar.getDownPayment().toString()));
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "贷款额");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,(saleCar.getLoanCost()==null?"":saleCar.getLoanCost().toString()));
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"分期数");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,(saleCar.getNper()==null?"":saleCar.getNper().toString()));
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"月供");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,(saleCar.getMonthPayment()==null?"":saleCar.getMonthPayment().toString()));
					++rowNum;
					
					rowData=bookTable.insertNewTableRow(rowNum);
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"按揭代办服务费");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, (saleCar.getFinancePoundage()==null?"":saleCar.getFinancePoundage().toString()));
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"金融公司");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,saleCar.getFinancecompanyName(),true);
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"其他费用",true);
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"",true);
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
					++rowNum;
					
					rowData=bookTable.insertNewTableRow(rowNum);
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "首付款合计");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, 
							(saleCar.getDownPaymentSum()==null?"":saleCar.getDownPaymentSum().toString()),true);
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "大写",true);
					WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"",true);
					for (int i = 0; i < 5; i++) {
						WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
					}
					++rowNum;
				}
			}
			
			rowData=bookTable.insertNewTableRow(rowNum);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "合同总额");
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, 
					(bookModel.getSumAmount()==null?"":bookModel.getSumAmount().toString()),true);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "大写",true);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"",true);
			for (int i = 0; i < 5; i++) {
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			}
			++rowNum;
			
			rowData=bookTable.insertNewTableRow(rowNum);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "定金");
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, 
					(bookModel.getDeposit()==null?"":bookModel.getDeposit().toString()),true);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"余款",true);
			
			
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,
					(bookModel.getResidue()==null?"":bookModel.getResidue().toString()),true);
			
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "大写",true);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"",true);
			for (int i = 0; i < 5; i++) {
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			}
			++rowNum;
			
			rowData=bookTable.insertNewTableRow(rowNum);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "合同条款",true);
			for (int i = 0; i < 9; i++) {
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			}
			++rowNum;
			
			rowData=bookTable.insertNewTableRow(rowNum);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "",true);
			for (int i = 0; i < 9; i++) {
				WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			}
			++rowNum;
			
			rowData=bookTable.insertNewTableRow(rowNum);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "供方代表签字");
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"",true);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"电话",true);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"",true);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "");
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "购方代表签字",true);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"",true);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"电话",true);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"",true);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor, "");
			++rowNum;
			
			rowData=bookTable.insertNewTableRow(rowNum);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"              年        月       日",true);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"              年        月        日",true);
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			WordTableCreateUtil.ctreateCell(rowData.addNewTableCell(), 0, headBgColor,"");
			++rowNum;
			bookTable.removeRow(rowNum);
		}
		return workdocx;
	}

}
